package com.briup.client;


import org.apache.log4j.Logger;
import com.briup.entity.Environment;


import java.io.*;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import static com.briup.util.backupUtil.loadBackup;
import static com.briup.util.backupUtil.saveBackup;

/**
 * @Auther: vanse(lc)
 * @Date: 2024/7/12-07-12-16:51
 * @Description：采集数据 -》 文件 -》换成环境数据
 * 100|101|2|16|1|3|5d606f7802|1|1516323596029
 * Environment 9个属性
 */
public class Gather {
    String filePath = "1";
    public Gather() {
    }
    public List<Environment> gather(){
        List<Environment> list = new ArrayList<>();
        int count = 0;
        Logger logger = Logger.getLogger(getClass());
        logger.debug("开始读取文件"+filePath);
        try {
            BufferedReader br= new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream(filePath)));
            br.skip(loadBackup(filePath)+2);//‘\n’的长度为2
            String line;
            while ((line = br.readLine())!= null) {
                String[] arr = line.split("\\|");
                Environment e = new Environment();
                e.setSrcId(arr[0]);
                e.setDesId(arr[1]);
                e.setDevId(arr[2]);
                e.setSersorAddress(arr[3]);
                e.setCount(Integer.parseInt(arr[4]));
                e.setCmd(arr[5]);
                e.setStatus(Integer.parseInt(arr[7]));
                e.setGather_date(new Timestamp(Long.parseLong(arr[8])));
                switch (arr[3]){
                    case "16":{
                        e.setName("温度");
                        BigInteger h = new BigInteger(arr[6].substring(0,4), 16);
                        float Temperature = (float) ((h.intValue() * 0.00268127) - 46.85);
                        e.setData(Temperature);
                        list.add(e);
                        count++;
                        Environment e2 = new Environment();
                        e2.setSrcId(arr[0]);
                        e2.setDesId(arr[1]);
                        e2.setDevId(arr[2]);
                        e2.setSersorAddress(arr[3]);
                        e2.setCount(Integer.parseInt(arr[4]));
                        e2.setCmd(arr[5]);
                        e2.setStatus(Integer.parseInt(arr[7]));
                        e2.setGather_date(new Timestamp(Long.parseLong(arr[8])));
                        e2.setName("湿度");
                        BigInteger a = new BigInteger(arr[6].substring(4, 8), 16);
                        float Humidity = (float) ((a.intValue() * 0.00190735) - 6);
                        e2.setData(Humidity);
                        list.add(e2);
                        count++;
                        break;
                    }
                    case "256":{
                        e.setName("光照");
                        e.setData(Integer.parseInt(arr[6].substring(0,4), 16));
                        list.add(e);
                        count++;
                        break;
                    }
                    case "1280":{
                        e.setName("二氧化碳");
                        e.setData(Integer.parseInt(arr[6].substring(0,4), 16));
                        list.add(e);
                        count++;
                        break;
                    }
                }
            }
            logger.debug("读取文件"+filePath+"完成，共"+count+"条数据");
            saveBackup(filePath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return list;
    }


//    public static void main(String[] args) {
//        Gather g = new Gather();
//        List<Environment> list = g.gather();
//        System.out.println(list.size());
//    }

}
